package org.zongf.learn.grpc.service;

import com.learn.grpc.grpc.HelloServiceGrpc;
import com.learn.grpc.hello.PingRequest;
import com.learn.grpc.hello.PingResponse;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.lognet.springboot.grpc.GRpcService;
import org.springframework.beans.factory.annotation.Value;

import java.time.LocalDateTime;

/**
 * @author zongf
 * @since 2023-07-06
 **/
@GRpcService
public class HelloServiceGrpcImpl extends HelloServiceGrpc.HelloServiceImplBase {

    @Value("${grpc.port}")
    private Integer grpcPort;

    @Override
    public void ping(PingRequest request, StreamObserver<PingResponse> responseObserver) {

        String name = request.getContent();

        String response = String.format("[%s] responseServer:%S, name:%s", LocalDateTime.now(), grpcPort, name);

        PingResponse pingResponse = PingResponse.newBuilder()
                .setData(response)
                .build();

        responseObserver.onNext(pingResponse);
        responseObserver.onCompleted();
    }
}
